/* Userspace gpio I/O library. 
 * author: maciek gajewski
 *  date: 14.02.2005
 *  license: GPL
 */
/** @file libgpiop.h userspace gpio i/o and manipulation library */
 
/** @mainpage  Library of handy userspace GPIO routines
 	
	Implements: gpio line state (function, direction and signal level)
	setting and quering, and gpio-based bus emulation.
	
	Go to libgpio.h file documentation for API details
 */

#ifndef LIB_GPIO_H
#define LIB_GPIO_H

#ifdef __cplusplus
	extern "C" { 
#endif /* _cplusplus */

/* line state constants gpio */
#define LG_GPIO_IN		0
#define LG_GPIO_OUT		1
#define LG_GPIO_DONTCARE	0xff

#define LG_FN_GPIO		0
#define LG_FN_AF1		1
#define LG_FN_AF2		2
#define LG_FN_AF3		3
/* don't change anything*/
#define LG_FN_DONTCARE	0xff

/** line state description  */
typedef struct  gpio_state_struct{
	unsigned char function; 	/**< function: LG_FN_GPIO-gpio, LG_FN_AF[1-3] - alternative, LG_FN_DONTCARE - no change */
	unsigned char direction;	/**< direction: LG_GPIO_IN, LG_GPIO_OUT, LG_GPIO_DONTCARE - no change */
	unsigned char state;		/**< output state:, 0, 1 or LG_GPIO_DONTCARE */
} gpio_state;


/* methods */
/** Library initialization. It HAS to be called before any other operation */
int gpio_init();

/** Free resources allocated by gpio_init(). No operation can be performed after calling the function*/
int gpio_shutdown();

/** Sets gpio line state. 
	@param line line number 
	@param state pointer to line state description
	@return 0 on OK, non-zero on error */
int gpio_set (int line, gpio_state* state);

/** Gets gpio line state.
	@param line line number
	@param state pointer to buffer recieving line state
	@return 0 on OK, non-zero on error */
int gpio_get(int line, gpio_state* state);

/* -------------------- bus ---------------------- */

/** bus handler type */
typedef void* gpio_bus;

/** constant data line width*/
#define LG_BUS_DATALINES	8
/** max addr bus width */
#define LG_BUS_MAX_ADDRLINES	8

/** Creates bus.
	@param data eight data lines
	@param addrnum addr lines number (max LG_BUS_MAX_ADDRLINES)
	@param addr addr lines
	@param wr wr (write-enable) line. inverted (low: active)
	@param rd rd (read-enable) line. inverted (low: active)
	@param cs cs (chip-select) line. inverted (low: active)
	@return created bus handler, or NULL on error */
gpio_bus gpio_bus_create(int data[LG_BUS_DATALINES], int addrnum, int* addr, int wr, int rd, int cs);

/** Free resources used by bus */
void gpio_bus_close(gpio_bus bus);

/** Writes single byte by the bus
	@param bus bus handler
	@param addr target addr 
	@param data byte to write
	@return 00 on OK, non-zero on error */
int gpio_bus_write(gpio_bus bus, char addr, char data);

/** Reads byte from the bus 
	@param bus bus handler
	@param add address
	@return readed byte, or -1 on error */
int gpio_bus_read(gpio_bus, char addr);

/** Reads multiple bytes from bus. Without incrementing address!
	@param bus bus handler
	@param addr address
	@param buf bufor for received data
	@param size number of bytes to read
	@return number of bytes readed, or -1 on error */
int gpio_bus_multiread(gpio_bus handler, char addr, char* buf, int size);


#ifdef __cplusplus
	} /* extern "C" */
#endif /* _cplusplus */

#endif /* LIB_GPIO_H */


